package com.sunny.ihealth.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.sunny.ihealth.BaseActivity;
import com.sunny.ihealth.R;
import com.sunny.ihealth.data.BmobManager;
import com.sunny.ihealth.data.PreferencesHelper;
import com.sunny.ihealth.entity.app.SPUserInfo;
import com.sunny.ihealth.entity.bmob.UserInfo;

public class SplashActivity extends BaseActivity {

    private static final long SPLASH_TIME = 2 * 1000L;

    /**
     * 软件进入模式
     */
    private int mode;
    /**
     * 用户资料
     */
    private SPUserInfo spUserInfo;
    /**
     * 回调是否成功
     */
    private boolean callBackDone = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        new Thread(new Runnable() {
            @Override
            public void run() {
                long time = System.currentTimeMillis();
                initUserState();

                //等待回调结束
                while (!callBackDone) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }


                //至少显示2秒
                time = SPLASH_TIME - (System.currentTimeMillis() - time);
                if (time > 0) {
                    try {
                        Thread.sleep(time);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }


                Intent intent = new Intent();
                //intent.putExtra(PreferencesHelper.SP_MODE, mode);
                if (mode == PreferencesHelper.MODE_FIRST_USE) {
                    intent.setClass(SplashActivity.this, VisitorActivity.class);
                } else {
                    intent.setClass(SplashActivity.this, MainActivity.class);
                    //intent.putExtra("data", spUserInfo);
                }

                startActivity(intent);
                finish();
            }
        }).start();
    }


    /**
     * sp name:user_data
     * 使用模式mode
     * 账号username
     * 密码password
     */
    private void initUserState() {
        mode = getPreferencesHelper().getMode();
        if (mode == PreferencesHelper.MODE_REGISTER_USER) {
            spUserInfo = getPreferencesHelper().getSPUserInfo();
            if (spUserInfo == null) {
                mode = PreferencesHelper.MODE_VISITOR;
                getBmob().logout();
            } else {  //注册用户
                login();
                return;
            }
        } else {
            getBmob().logout();
        }
        callBackDone = true;
    }


    //登陆
    private void login() {
        getBmob().login(spUserInfo.getUsername(), spUserInfo.getPassword(), new BmobManager.Action1<UserInfo>() {
            @Override
            public void onCompleted(UserInfo userInfo) {
                if (userInfo != null) { //登陆成功，更新本地数据
                    getPreferencesHelper().setUserInfo(userInfo);
                } else {   //登陆失败
                    getPreferencesHelper().setMode(PreferencesHelper.MODE_VISITOR);
                    getBmob().logout();
                }
                callBackDone = true;
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
